package org.ioc.com.mybatis_test1.mapper;

import org.apache.ibatis.annotations.*;
import org.ioc.com.mybatis_test1.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    //通过mybatis注解增加数据
    @Insert("insert into userinfo(username,age,password,gender) values (#{username},#{age},#{password},#{gender})")
    Integer insert(UserInfo userInfo);
    //通过mybatis注解删除数据
    @Delete("delete from userinfo where id=#{id}")
    Integer delete(Integer id);
    //通过mybatis注解修改数据
    @Update("update userinfo set username=#{username} where id=#{id}")
    Integer update(Integer id,String username);

   /*
    //通过mybatis注解查询数据;
    //我们看到打印的结果不正确
    //是因为数据库的字段和Java对象的属性对应的我们可以获取到值,但是不对应的直接为空
    这里我们有三种解决方式
    1.改别名---就是将数据库字段名用as改成和Java对象一样的名字
    2.结果映射
    3.驼峰转换
    */
    @Select("select * from userinfo")
    List<UserInfo> selectAll();

    //1.改别名
    @Select("select id,username,age,phone,gender,create_time as createTime,update_time as updateTime,delete_flag as deleteFlag from userinfo;")
    List<UserInfo> selectAll2();
    //2.结果映射
    @Results(id="resultMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> selectAll3();

    @ResultMap("resultMap")
    @Select("select * from userinfo")
    List<UserInfo> selectAll4();

    //3.驼峰转换(这里需要加一个配置文件:map-underscore-to-camel-case: true)
    @Select("select * from userinfo")
    List<UserInfo> selectAll5();

}
